package com.aloda.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.annotation.AdminAuthCheck;
import com.aloda.annotation.OperateLogAnno;
import com.aloda.query.PicPageDTO;
import com.aloda.result.PageResult;
import com.aloda.result.Result;
import com.aloda.service.PictureService;
import com.aloda.vo.PicPageVO;
import com.aloda.vo.PicVO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 图片资源controller
 * @author aloda
 * @date 2024/12/19
 */
@RestController
@RequestMapping("/admin/pic")
public class PictureController {

    @Resource
    private PictureService pictureService;

    /**
     * 分页查询
     * @param picPageDTO
     * @return
     */
    @AdminAuthCheck
    @PostMapping("/page")
    public Result<PageResult<PicPageVO>> pageQuery(@RequestBody PicPageDTO picPageDTO) {
        PageResult<PicPageVO> pageResult = pictureService.pageQuery(picPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @AdminAuthCheck
    @GetMapping("/{id}")
    public Result<PicVO> getById(@PathVariable Long id) {
        return Result.success(BeanUtil.copyProperties(pictureService.getById(id),PicVO.class));
    }

    /**
     * 删除日志
     * @param ids
     * @return
     */
    @OperateLogAnno(category = "删除图片资源记录日志")
    @AdminAuthCheck
    @DeleteMapping
    public Result delete(@RequestBody List<Long> ids) {
        return Result.success(pictureService.removeBatchByIds(ids));
    }

    /**
     * 导出图片信息到Excel
     * @param response HttpServletResponse
     * @param ids 图片ID列表
     */
    @OperateLogAnno(category = "导出图片信息到Excel")
    @AdminAuthCheck
    @PostMapping("/export")
    public void exportPictureToExcel(HttpServletResponse response, @RequestBody List<Long> ids) {
        // 调用服务层方法导出图片信息
        pictureService.exportPictureToExcel(response, ids);
    }
}
